namespace RivercrabNet
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Drawing;
    using System.Drawing.Drawing2D;

    public class IndicatorOscillator : BaseIndicator
    {
        private string closeMA = ("OSC.CloseMA" + Common.GetGuid());
        private LineShape tlsMaOsc;
        private LineShape tlsOsc;

        public IndicatorOscillator()
        {
            base.IndType = IndicatorType.OSC;
            base.IndName = "OSC";
            base.IndDesc = "摆动量";
            base.IndPeriod.Add(new IndicatorPeriod(0, "N", 10.0));
            base.IndPeriod.Add(new IndicatorPeriod(1, "M", 6.0));
            base.SourceDic["Close"] = "";
            base.fields = new string[] { "OSC", "MAOSC" };
        }

        public override bool Contains(object obj)
        {
            if ((obj != this.tlsOsc) && (obj != this.tlsMaOsc))
            {
                return false;
            }
            return true;
        }

        public override double[] Get(int r)
        {
            double curValue = Convert.ToDouble(base.Crab.DataSource.Rows[r][base.SourceDic["Close"]]);
            double num2 = Common.CalcuteMovingAvg(r, (int) base.IndPeriod[0].ParamValue, this.closeMA, this.closeMA, curValue, base.Crab.DataSource);
            double num3 = curValue - num2;
            double num4 = Common.CalcuteMovingAvg(r, (int) base.IndPeriod[1].ParamValue, this.tlsMaOsc.Name, this.tlsOsc.Name, num3, base.Crab.DataSource);
            return new double[] { num2, num3, num4 };
        }

        public override List<BaseShape> GetShapeList()
        {
            return new List<BaseShape> { this.tlsOsc, this.tlsMaOsc };
        }

        public override void Remove()
        {
            base.Crab.DeleteShape(this.TlsOsc);
            base.Crab.DeleteShape(this.TlsMaOsc);
            base.Crab.DataSource.Columns.Remove(this.CloseMA);
        }

        public override bool Set(int r, string target)
        {
            try
            {
                DataRow row = base.Crab.DataSource.Rows[r];
                foreach (string str in base.SourceDic.Values)
                {
                    if ((str == target) || (target == null))
                    {
                        foreach (string str2 in base.SourceDic.Values)
                        {
                            if (row[str2] is DBNull)
                            {
                                return false;
                            }
                        }
                        double[] numArray = this.Get(r);
                        base.Crab.SetValue(this.CloseMA, numArray[0], r);
                        base.Crab.SetValue(this.TlsOsc.Name, numArray[1], r);
                        base.Crab.SetValue(this.TlsMaOsc.Name, numArray[2], r);
                        return true;
                    }
                }
                return false;
            }
            catch
            {
                return false;
            }
        }

        public override void SetParam(string[] fields, string[] source, double[] periods, int panelID)
        {
            base.SourceDic["Close"] = source[0];
            base.IndPeriod[0].ParamValue = periods[0];
            base.IndPeriod[1].ParamValue = periods[1];
            this.TlsOsc = base.Crab.AddLine(fields[0], panelID);
            base.Crab.SetLine(fields[0], fields[0], Color.White, 1, 0.01, 2, DashStyle.Solid);
            this.TlsMaOsc = base.Crab.AddLine(fields[1], panelID);
            base.Crab.DataSource.Columns.Add(this.CloseMA);
        }

        public string CloseMA
        {
            get
            {
                return this.closeMA;
            }
            set
            {
                this.closeMA = value;
            }
        }

        public LineShape TlsMaOsc
        {
            get
            {
                return this.tlsMaOsc;
            }
            set
            {
                this.tlsMaOsc = value;
            }
        }

        public LineShape TlsOsc
        {
            get
            {
                return this.tlsOsc;
            }
            set
            {
                this.tlsOsc = value;
            }
        }
    }
}

